﻿
Imports System.IO
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

Public Class image
    Inherits requestProcessor

    Public Const _pathidentity As String = "/image"
    Protected _reloaddelay As Int64
    Protected _imagelink As String

    Public Function reloaddelay() As Int64
        Return _reloaddelay
    End Function

    Public Function imagelink() As String
        Return _imagelink
    End Function

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial()
        _reloaddelay = config.parameter(configNaming.image.name, configNaming.image.reloaddelay _
                                        , configNaming.defaultValue.image.reloaddelay)
        _imagelink = config.parameter(configNaming.image.name, configNaming.image.link _
                                      , configNaming.defaultValue.image.link)
    End Sub

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.body().id = "id_image"
        page.title().text = applicationSign() + " - image"

        Dim csslink As csslink = Nothing
        csslink = New csslink(buff)
        csslink.href = csslinkhref
        page.head().addchild(csslink)

        Dim delayReloader As delayReloader = Nothing
        delayReloader = New delayReloader(buff)
        delayReloader.delay = reloaddelay()
        page.head().addchild(delayReloader)

        Dim img As img = Nothing
        img = New img(buff)
        img.src = imagelink()
        img.width = "100%"
        page.body().addchild(img)

        Dim reloader As reloader = Nothing
        reloader = New reloader(buff)
        reloader.id = "id_image_reload"
        reloader.text = "try another image"
        page.body().addchild(reloader)

        context.setstatuscode(Net.HttpStatusCode.OK, "OK")
        context.setcontentEncoding(encoding.utf8)
        context.setcontentType(network.http.contentType.texthtmlUtf8)

        If page.render() Then
            Return True
        Else
            raiseError("cannot render xhtmlpage.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
